<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class News extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'));
        $this->data['controller'] = 'news';         
        $this->load->model('Mnews');
    }
    
    public function index()
    {
        $this->data['id_active'] = $this->getParamUri(3,2);
        $this->load->library('pagination');
        $config['per_page'] = 5;
        $this->data['page'] = $this->data['pageg'] = $this->getParamUri(3,2);
        $data = $this->Mnews->getNewsDB($config['per_page'],$this->getParamUri(3,2),2);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,2);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
	    $this->data['action'] = 'index';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function editnews()
    {
        $this->load->library('form_validation');
        
        $this->data['id'] = $this->getParamInt('id');
        $this->data['data'] = $this->Mnews->getNewsEdit($this->data['id']);
        
        $this->form_validation->set_rules('namePro', 'Tên khuyến mãi', 'required');
        $this->form_validation->set_rules('nameProSeo', 'Tên khuyến mãi SEO', 'required');
        $this->data['selectCate'] = $this->Mnews->danhmucGroupotp($this->data['data']->id_c, 2);
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mnews->editNewsDb($this->data['id'],$img);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa Tin tức thành công");window.location.href="'.$this->data['baseurl'].'manage/news.html";</script>');
            }
        }
        $this->data['action'] = 'editnews';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function addnews()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('namePro', 'Tiêu đề', 'required');
        $this->form_validation->set_rules('nameProSeo', 'Tiêu đề SEO', 'required');
        $this->data['selectCate'] = $this->Mnews->danhmucGroupotp('', 2);
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mnews->addNewsDb($img,2);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm Tin tức thành công");window.location.href="'.$this->data['baseurl'].'manage/news.html";</script>');
            }
        }
        $this->data['action'] = 'addnews';
        $this->load->view('temp/layout', $this->data);
    }
    
    private function selectType($id = 0)
    {
        $arr = array('Cốc', 'Cái', 'Bịch', 'Chục', 'Hộp', 'Thùng', 'Chiếc', 'Lô', 'Gói', 'tá', 'Kg', 'Gam');
        $html  = '';
        foreach($arr AS $el=>$le)
        {
            $ids = ($el + 1);
            if($id == $ids)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$ids.'" '.$selected.'>'.$le.'</option>';
        }
        return $html;
    }
    
    public function deletenews()
    {
        $run = $this->Mnews->deleteProductDB($this->session->userdata('username'));
        if($run == 200)
        {
            die('<meta charset="utf-8"><script>alert("Xóa tin tức thành công");window.location.href="'.$this->data['baseurl'].'manage/news.html";</script>');
        }
        
    }
    
    /**
     *
     * @Thêm khuyến mãi 
     * 
     */
    
    public function khuyenmai()
    {
        $this->data['id_active'] = $this->getParamUri(3,2);
        $this->load->library('pagination');
        $config['per_page'] = 5;
        $this->data['page'] = $this->data['pageg'] = $this->getParamUri(3,2);
        $data = $this->Mnews->getNewsDB($config['per_page'],$this->getParamUri(3,2),3);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,2);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
	    $this->data['action'] = 'khuyenmai';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function editkhuyenmai()
    {
        $this->load->library('form_validation');
        
        $this->data['id'] = $this->getParamInt('id');
        $this->data['data'] = $this->Mnews->getNewsEdit($this->data['id']);
        
        $this->form_validation->set_rules('namePro', 'Tên khuyến mãi', 'required');
        $this->form_validation->set_rules('nameProSeo', 'Tên khuyến mãi SEO', 'required');
        $this->data['selectCate'] = $this->Mnews->danhmucGroupotp($this->data['data']->id_c, 3);
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mnews->editNewsDb($this->data['id'],$img);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa Khuyến mãi thành công");window.location.href="'.$this->data['baseurl'].'manage/khuyenmai.html";</script>');
            }
        }
        $this->data['action'] = 'editkhuyenmai';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function addkhuyenmai()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('namePro', 'Tiêu đề', 'required');
        $this->form_validation->set_rules('nameProSeo', 'Tiêu đề SEO', 'required');
        $this->data['selectCate'] = $this->Mnews->danhmucGroupotp('', 3);
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mnews->addNewsDb($img,3);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm khuyến mãi thành công");window.location.href="'.$this->data['baseurl'].'manage/khuyenmai.html";</script>');
            }
        }
        $this->data['action'] = 'addkhuyenmai';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function deletekhuyenmai()
    {
        $run = $this->Mnews->deleteProductDB($this->session->userdata('username'));
        if($run == 200)
        {
            die('<meta charset="utf-8"><script>alert("Xóa tin khuyến mãi thành công");window.location.href="'.$this->data['baseurl'].'manage/khuyenmai.html";</script>');
        }
        
    }
    
    
    /**
     *
     * @Quan ly tuyen dung
     * 
     */
    
    public function tuyendung()
    {
        $this->data['id_active'] = $this->getParamUri(3,2);
        $this->load->library('pagination');
        $config['per_page'] = 5;
        $this->data['page'] = $this->data['pageg'] = $this->getParamUri(3,2);
        $data = $this->Mnews->getNewsDB($config['per_page'],$this->getParamUri(3,2),4);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,2);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
	    $this->data['action'] = 'tuyendung';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function edittuyendung()
    {
        $this->load->library('form_validation');
        
        $this->data['id'] = $this->getParamInt('id');
        $this->data['data'] = $this->Mnews->getNewsEdit($this->data['id']);
        
        $this->form_validation->set_rules('namePro', 'Tiêu đề tuyển dụng', 'required');
        $this->form_validation->set_rules('nameProSeo', 'Tên tiêu đề SEO', 'required');
        $this->data['selectCate'] = $this->Mnews->danhmucGroupotp($this->data['data']->id_c, 4);
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mnews->editNewsDb($this->data['id'],$img);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa Tin tuyển dụng thành công");window.location.href="'.$this->data['baseurl'].'manage/tuyendung.html";</script>');
            }
        }
        $this->data['action'] = 'edittuyendung';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function addtuyendung()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('namePro', 'Tiêu đề tuyển dụng', 'required');
        $this->form_validation->set_rules('nameProSeo', 'Tiêu đề SEO', 'required');
        $this->data['selectCate'] = $this->Mnews->danhmucGroupotp('', 4);
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mnews->addNewsDb($img,4);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm tin tuyển dụng thành công");window.location.href="'.$this->data['baseurl'].'manage/tuyendung.html";</script>');
            }
        }
        $this->data['action'] = 'addtuyendung';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function deletetuyendung()
    {
        $run = $this->Mnews->deleteProductDB($this->session->userdata('username'));
        if($run == 200)
        {
            die('<meta charset="utf-8"><script>alert("Xóa tin tuyển dụng thành công");window.location.href="'.$this->data['baseurl'].'manage/tuyendung.html";</script>');
        }
        
    }
    
     /**
     *
     * @Thêm khuyến mãi 
     * 
     */
    
    public function dichvu()
    {
        $this->data['id_active'] = $this->getParamUri(3,2);
        $this->load->library('pagination');
        $config['per_page'] = 5;
        $this->data['page'] = $this->data['pageg'] = $this->getParamUri(3,2);
        $data = $this->Mnews->getNewsDB($config['per_page'],$this->getParamUri(3,2),6);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,2);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
	    $this->data['action'] = 'dichvu';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function editdichvu()
    {
        $this->load->library('form_validation');
        
        $this->data['id'] = $this->getParamInt('id');
        $this->data['data'] = $this->Mnews->getNewsEdit($this->data['id']);
        
        $this->form_validation->set_rules('namePro', 'Tên dịch vụ', 'required');
        $this->form_validation->set_rules('nameProSeo', 'Tên dịch vụ SEO', 'required');
        $this->data['selectCate'] = $this->Mnews->danhmucGroupotp($this->data['data']->id_c, 6);
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mnews->editNewsDb($this->data['id'],$img);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa dịch vụ thành công");window.location.href="'.$this->data['baseurl'].'manage/dichvu.html";</script>');
            }
        }
        $this->data['action'] = 'editdichvu';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function adddichvu()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('namePro', 'Tiêu đề', 'required');
        $this->form_validation->set_rules('nameProSeo', 'Tiêu đề SEO', 'required');
        $this->data['selectCate'] = $this->Mnews->danhmucGroupotp('', 6);
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mnews->addNewsDb($img,6);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm dịch vụ thành công");window.location.href="'.$this->data['baseurl'].'manage/dichvu.html";</script>');
            }
        }
        $this->data['action'] = 'adddichvu';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function deletedichvu()
    {
        $run = $this->Mnews->deleteProductDB($this->session->userdata('username'));
        if($run == 200)
        {
            die('<meta charset="utf-8"><script>alert("Xóa dịch vụ thành công");window.location.href="'.$this->data['baseurl'].'manage/dichvu.html";</script>');
        }
        
    }
    
    
     /**
     *
     * @Quan ly các trang khác
     * 
     */
    
    public function other()
    {
        $this->data['id_active'] = $this->getParamUri(3,2);
        $this->load->library('pagination');
        $config['per_page'] = 5;
        $this->data['page'] = $this->data['pageg'] = $this->getParamUri(3,2);
        $data = $this->Mnews->getNewsDB($config['per_page'],$this->getParamUri(3,2),5);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,2);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
	    $this->data['action'] = 'other';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function editother()
    {
        $this->load->library('form_validation');
        
        $this->data['id'] = $this->getParamInt('id');
        $this->data['data'] = $this->Mnews->getNewsEdit($this->data['id']);
        
        $this->form_validation->set_rules('namePro', 'Tiêu đề trang', 'required');
        $this->form_validation->set_rules('nameProSeo', 'Tên tiêu đề SEO', 'required');
        $this->data['selectCate'] = $this->Mnews->danhmucGroupotp($this->data['data']->id_c, 5);
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mnews->editNewsDb($this->data['id'],$img);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa trang thành công");window.location.href="'.$this->data['baseurl'].'manage/other.html";</script>');
            }
        }
        $this->data['action'] = 'editother';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function addother()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('namePro', 'Tiêu đề trang', 'required');
        $this->form_validation->set_rules('nameProSeo', 'Tiêu đề SEO', 'required');
        $this->data['selectCate'] = $this->Mnews->danhmucGroupotp('', 5);
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mnews->addNewsDb($img,5);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm trang thành công");window.location.href="'.$this->data['baseurl'].'manage/other.html";</script>');
            }
        }
        $this->data['action'] = 'addother';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function deleteother()
    {
        $run = $this->Mnews->deleteProductDB($this->session->userdata('username'));
        if($run == 200)
        {
            die('<meta charset="utf-8"><script>alert("Xóa trang thành công");window.location.href="'.$this->data['baseurl'].'manage/other.html";</script>');
        }
        
    }
    
}
